﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ComClassLibrary
{
    public class AppDirectory
    {
        private string _localSavePath = @"D:\Data";
        private string _localExportPath = @"D:\ExportData";

        private System.IO.DirectoryInfo _currDirInfo = null;

        public string LocalSavePath
        {
            get { return _localSavePath; }
            set { _localSavePath = value; }
        }

        private string _englishName="";
        public string EnglishName
        {
            get { return _englishName; }
            set { _englishName = value; }
        }

        private string _certificateIdNo = "";
        public string CertificateIdNo
        {
            get { return _certificateIdNo; }
            set { _certificateIdNo = value; }
        }

        /// <summary>
        /// 本地共享目录（让其他电脑访问）
        /// </summary>
        private string _localSharedPath = @"D:\SharedData";
        public string LocalSharedPath
        {
            get { return _localSharedPath; }
            set { _localSharedPath = value; }
        }

        private void CreatRootDirectory()
        {
            _strRootSavePath = string.Format(@"{0}\{1}\{2}",
                   _localSavePath,
                   System.DateTime.Now.ToString("yyyyMM"),
                   System.DateTime.Now.ToString("dd"));
            _currDirInfo = new DirectoryInfo(_strRootSavePath);
            if (!_currDirInfo.Exists)
            {
                _currDirInfo.Create();
                //Logger.Add("创建新目录:" + _strRootSavePath);
            }
        }

        private void CreatExportDirectory()
        {
            _strExportPath = string.Format(@"{0}\{1}\{2}",
                _localExportPath,
                System.DateTime.Now.ToString("yyyyMM"),
                System.DateTime.Now.ToString("dd"));
            _currDirInfo = new DirectoryInfo(_strExportPath);
            if (!_currDirInfo.Exists)
            {
                _currDirInfo.Create();
                //Logger.Add("创建新目录:" + _strRootSavePath);
            }
        }

        /// <summary>
        /// 检查并更新当日目录
        /// </summary>
        /// <returns></returns>
        private void CheckAndUpdateRootPath()
        {
            if (_currDirInfo == null)
            {
                CreatRootDirectory();
            }
            if (string.IsNullOrEmpty(_strRootSavePath))
            {
                CreatRootDirectory();
            }
            if (!Directory.Exists(_strRootSavePath))
            {
                CreatRootDirectory();
            }
            if (!_currDirInfo.CreationTime.Date.Equals(System.DateTime.Now.Date))
            {
                CreatRootDirectory();
            }
        }

        /// <summary>
        /// 检查并更新当日目录
        /// </summary>
        /// <returns></returns>
        private void CheckAndUpdateExportPath()
        {
            if (_currDirInfo == null)
            {
                CreatExportDirectory();
            }
            if (string.IsNullOrEmpty(_strExportPath))
            {
                CreatExportDirectory();
            }
            if (!Directory.Exists(_strExportPath))
            {
                CreatExportDirectory();
            }
            if (!_currDirInfo.CreationTime.Date.Equals(System.DateTime.Now.Date))
            {
                CreatExportDirectory();
            }
        }

        /// <summary>
        /// 当日照片保存的根目录
        /// </summary>
        private string _strRootSavePath = "";
        public string RootSavePath
        {
            get
            {
                CheckAndUpdateRootPath();
                return _strRootSavePath;
            }
        }


        /// <summary>
        /// 数据导出的根目录
        /// </summary>
        private string _strExportPath = "";
        public string ExportPath
        {
            get
            {
                CheckAndUpdateExportPath();
                return _strExportPath;
            }
        }

        /// <summary>
        /// 手写签名图片保存目录
        /// </summary>     
        public string Signature
        {
            get
            {
                return CheckAndCreatPath("SIGNATURE");
            }
        }

        /// <summary>
        /// 手写签名图片保存目录
        /// </summary>     
        public string HeadImage
        {
            get
            {
                return CheckAndCreatPath("HeadImage");
            }
        }

        /// <summary>
        /// 临时文件目录
        /// </summary>     
        public string Temp
        {
            get
            {
                return CheckAndCreatPath("TEMP");
            }
        }

        /// <summary>
        /// 资料页拍照记录
        /// </summary>
        public string Materials
        {
            get
            {
                return CheckAndCreatPath("Materials");
            }
        }

        /// <summary>
        /// 停居留证件扫描目录
        /// </summary>
        public string ResidentVisa
        {
            get
            {
                return CheckAndCreatPath("ResidentVisa");
            }
        }

        /// <summary>
        /// 护照照片保存文件夹名称
        /// </summary>
        public string Passport
        {
            get
            {
                return CheckAndCreatPath("Passport");
            }
        }

        /// <summary>
        /// 回执照片保存文件夹名称
        /// </summary>
        public string Receipt
        {
            get
            {
                return CheckAndCreatPath("Receipt");
            }
        }

        /// <summary>
        /// 入境签章资料保存目录
        /// </summary>
        public string EntryStamp
        {
            get
            {
                return CheckAndCreatPath("EntryStamp");
            }
        }

           /// <summary>
        /// 现场人像保存目录
        /// </summary>
        public string ScenePortrait
        {
            get
            {
                return CheckAndCreatPath("ScenePortrait");
            }
        }

        /// <summary>
        /// 导出文件保存文件夹名称
        /// </summary>
        public string Export
        {
            get
            {
                return CheckAndCreatExportPath();
            }
        }

        private string CheckAndCreatPath(string name)
        {
            string path = Path.Combine(RootSavePath, string.Format("{0}_{1}", name, DateTime.Now.ToString("yyyyMMddHHmmss")));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                //Logger.Add("创建新目录:" + path);
            }
            return path;
        }

        private string CheckAndCreatExportPath()
        {
            string path = Path.Combine(ExportPath, string.Format("{0}_{1}_{2}", _certificateIdNo, _englishName,DateTime.Now.ToString("yyyyMMddHHmmss")));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                //Logger.Add("创建新目录:" + path);
            }
            return path;
        }

        //public override bool Save()
        //{
        //    base.Save();
        //    SaveField("LocalSavePath", _localSavePath);
        //    SaveField("LocalSharedPath", _localSharedPath);
        //    return true;
        //}

        //public override bool Load()
        //{
        //    base.Load();
        //    try
        //    {
        //        _localSavePath = LoadStringValue("LocalSavePath");
        //        _localSharedPath = LoadStringValue("LocalSharedPath");
        //    }
        //    catch (Exception ex)
        //    {
        //        string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
        //        ErrorMessages.Add(strMsg);
        //        //Logger.WriteException(ex.ToString());
        //        return false;
        //    }
        //    return true;
        //}

        //public override IniConfigBase LoadFrom(string path)
        //{
        //    PhotoDirectory obj = new PhotoDirectory();
        //    obj._strConfigPath = path;
        //    if (obj.Load())
        //    {
        //        return obj;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("照片工作目录选项::");
            builder.Append("[本地工作目录]: " + _localSavePath);
            builder.Append("[本地共享目录]: " + _localSharedPath);
            return builder.ToString();
        }
    }
}
